
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.dto.DistributionUserBindDTO;
import com.jf.cloud.distribution.service.DistributionUserBindService;
import com.jf.cloud.distribution.vo.DistributionUserBindVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 分销员绑定关系
 *
 * @author zz
 * @date 2021-08-09 14:14:09
 */
@RestController("platformDistributionUserBindController")
@RequestMapping("/p/distribution_user_bind")
@Tag(name = "platform-分销员绑定关系")
public class DistributionUserBindController {

    @Autowired
    private DistributionUserBindService distributionUserBindService;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销员绑定关系列表" , description = "分页获取分销员绑定关系列表")
    public ServerResponseEntity<PageVO<DistributionUserBindVO>> page(@Valid PageDTO pageDTO,@ParameterObject DistributionUserBindDTO distributionUserBindDTO) {
        PageVO<DistributionUserBindVO> page = distributionUserBindService.page(pageDTO, distributionUserBindDTO);
        if (BooleanUtil.isFalse(permission)){
            for (DistributionUserBindVO distributionUserBindVO : page.getList()) {
                if (StrUtil.isNotBlank(distributionUserBindVO.getNickName()) && PrincipalUtil.isMobile(distributionUserBindVO.getNickName())) {
                    distributionUserBindVO.setNickName(PhoneUtil.hideBetween(distributionUserBindVO.getNickName()).toString());
                }
                if (StrUtil.isNotBlank(distributionUserBindVO.getUserNickName()) && PrincipalUtil.isMobile(distributionUserBindVO.getUserNickName())) {
                    distributionUserBindVO.setUserNickName(PhoneUtil.hideBetween(distributionUserBindVO.getUserNickName()).toString());
                }
            }
        }
        return ServerResponseEntity.success(page);
    }
}
